import { ConfirmPlayerE2PService } from './services/confirmPlayerE2P.service';
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { EventToPlayerService } from './event-to-player.service';
import { CreateEventToPlayerDto } from './dto/create-event-to-player.dto';
import { UpdateEventToPlayerDto } from './dto/update-event-to-player.dto';
import {
  ApiOperation,
  ApiBody,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { BindPlayerE2PDto } from './dto/bind-player-e2p.dto';
import { BindPlayerService } from './services/bingPlayer.service';
import { Auth } from 'src/common/decorators/auth.decorator';
import { BindTeamPlayerE2PDto } from './dto/bind-team-player-e2p.dto';
import { EventToPlayerCacheService } from './event-to-player.cache.service';

@ApiBearerAuth() //
@Auth()
@Controller('event-to-player')
export class EventToPlayerController {
  constructor(
    private readonly eventToPlayerService: EventToPlayerService,
    private readonly confirmPlayerE2PService: ConfirmPlayerE2PService,
    private readonly bingPlayerService: BindPlayerService,
    private readonly eventToPlayerCacheService: EventToPlayerCacheService,
  ) {}

  @Post()
  create(@Body() createEventToPlayerDto: CreateEventToPlayerDto) {
    return this.eventToPlayerService.create(createEventToPlayerDto);
  }

  @Get()
  findAll() {
    return this.eventToPlayerService.findAll();
  }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.eventToPlayerService.findOne(+id);
  // }

  // @Patch(':id')
  // update(
  //   @Param('id') id: string,
  //   @Body() updateEventToPlayerDto: UpdateEventToPlayerDto,
  // ) {
  //   return this.eventToPlayerService.update(+id, updateEventToPlayerDto);
  // }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.eventToPlayerService.remove(+id);
  }

  @Post('ConfirmPlayerE2P')
  @ApiOperation({ summary: '确认球员的 E2P 配置' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        eventId: {
          type: 'number',
          description: '赛事的 ID',
        },
        confirmE2PType: {
          type: 'string',
          enum: ['random', 'custom'],
          description: '确认 E2P 的类型，可选值为 random 或 custom',
        },
        customE2PConfig: {
          type: 'object',
          additionalProperties: {
            $ref: '#/components/schemas/CustomConfirmE2PParam',
          },
          description:
            '自定义 E2P 配置，键为注册索引，值为 CustomConfirmE2PParam 类型',
        },
      },
      required: ['eventId', 'confirmE2PType', 'customE2PConfig'],
    },
  })
  async ConfirmPlayerE2P(
    @Body()
    param: {
      eventId: number;
      confirmE2PType: 'random' | 'custom';
      customE2PConfig: {
        [orderNum: number]: number[];
      };
    },
  ) {
    const { eventId, confirmE2PType, customE2PConfig } = param;
    await this.confirmPlayerE2PService.confirmPlayerE2P(
      eventId,
      confirmE2PType,
      customE2PConfig,
    );
  }
  @Post('bindPlayerToE2P')
  @ApiOperation({ summary: '将玩家绑定到 E2P 记录' })
  @ApiBody({
    type: BindPlayerE2PDto,
    description: '绑定玩家到 E2P 记录所需的参数',
  })
  async bindPlayerToE2P(@Body() param: BindPlayerE2PDto) {
    return await this.bingPlayerService.bindPlayerToE2P([param]);
  }
  @Post('bindTeamPlayerE2PDto')
  @ApiOperation({ summary: '绑定teamplayer E2P 记录' })
  @ApiBody({
    type: BindTeamPlayerE2PDto,
    description: '绑定玩家到 E2P 记录所需的参数',
  })
  async bindTeamPlayerE2PDto(@Body() param: BindTeamPlayerE2PDto) {
    // return await this.bingPlayerService.bindTeamPlayerRole([param]);
    /**
     * 1. 获取所有的子比赛
     * 2. 根据order 绑定player 到子比赛
     * 3. 更新子比赛
     */

    console.log(param);
  }
  @Get('getByEventId')
  @ApiOperation({ summary: '根据赛事ID获取赛事与玩家的关联记录' })
  @ApiQuery({
    name: 'eventId',
    type: Number,
    description: '赛事的ID',
    required: true,
  })
  async getByEventId(@Query('eventId') eventId: number) {
    // console.log('3333eventId', eventId);
    return await this.eventToPlayerCacheService.getListByEventId(eventId);
  }
}
